/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.remoting;

import java.io.DataInputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.tworen.demou.amf.Amf0Decode;
import cn.tworen.demou.remoting.cmd.BaseCmd;

/**
 * Created on 2007-2-4
 * 
 * Title : FlexRemoteServlet.java Description :
 * 
 * @author : LuJinYi
 * @version : 1.0
 * @Date : 2007-2-4 History :
 * 
 */
public class FlexRemoteServlet extends HttpServlet {
	private Amf0Decode decode = new Amf0Decode();
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(FlexRemoteServlet.class);

	public void init(ServletConfig servletConfig) throws ServletException {
		logger.debug("##################service init");

	}

	public void destroy() {
		logger.debug("##################service destroy");

	}

	public void service(HttpServletRequest req, HttpServletResponse res) {
		logger.debug("##################service invoke");
		logger.debug("ContentLength=" + req.getContentLength());
		logger.debug("ContextPath=" + req.getContextPath());
		logger.debug("method=" + req.getMethod());
		logger.debug("uri=" + req.getRequestURI());
		logger.debug("ServletPath=" + req.getServletPath());
		// while(req.getAttributeNames().hasMoreElements()){
		// logger.debug("attr=" + req.getAttributeNames().toString());
		// }
		// java.util.Iterator iterator =
		// req.getParameterMap().keySet().iterator();
		// while(iterator.hasNext()){
		// logger.debug("key=" + iterator.toString() + " value=" +
		// req.getParameter(iterator.toString()));
		// }
		// byte[] conten = new byte[req.getContentLength()];
		// try {
		// logger.debug("read content");
		// req.getInputStream().read(conten);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// decode.decodeCmd(null, buffer);
		try {
			BaseCmd cmd = new BaseCmd();
			DataInputStream dataInputStream = new DataInputStream(req
					.getInputStream());
			byte[] byteRead = new byte[req.getContentLength()];
			dataInputStream.read(byteRead);
			HexDump.dumpHexData("receview data", byteRead, byteRead.length);
			logger.debug("init RemoteDecode");
//			RemoteDecode rmoteDecode = new RemoteDecode();
//			rmoteDecode.readMessage(byteRead, cmd);
			
//			BigInteger big = new BigInteger(byteRead);
//			logger.debug(big.toString(16));
			
//			DataInputStream dataInputStream = new DataInputStream(req
//					.getInputStream());
//			int version = dataInputStream.readUnsignedShort();
//			cmd.setVersion(version);
//			logger.debug("version=" + version);
//			int headerCount = dataInputStream.readUnsignedShort();
//			logger.debug("headerCount=" + headerCount);
//
//			for (int i = 0; i < headerCount; i++) {
//				Header header = new Header();
//				cmd.getHeaders().add(header);
//				String name = dataInputStream.readUTF();
//				header.setName(name);
//				boolean mustUnderstand = dataInputStream.read() == 0?false:true;
//				header.setMustUnderstand(mustUnderstand);
//				int length = dataInputStream.readInt();
//				logger.debug("name=" + name + " understand=" + mustUnderstand + " lenght=" + length);
//				Object data = null;
//				try {
//					dataInputStream.skipBytes(length);
////					data = decode.readObject();
//				} catch (Exception ex) {
//					data = ex;
//				}
//				header.setValue(data);			
//			}
//
//			int bodyCount = dataInputStream.readUnsignedShort();
//			logger.debug("bodyCount=" + bodyCount);
//			for (int i = 0; i < bodyCount; i++) {
//				Body body = new Body();
//				cmd.getBodies().add(body);
//				String targetURI = dataInputStream.readUTF();
//				body.setTargetURI(targetURI);
//				String responseURI = dataInputStream.readUTF();
//				body.setResponseURI(responseURI);
//				int length = dataInputStream.readInt();
//				logger.debug("targetURI=" + targetURI + " responseURI=" + responseURI + " lenght=" + length);
//				logger.debug("available=" + dataInputStream.available());
//				byte[] bodybytes = new byte[length];
//				Object data = null;
//				try {
//					dataInputStream.read(bodybytes);
//					Buffer buffer = BufferFactory.wrap(bodybytes);
//					logger.debug(bodybytes[0]);
//					buffer.put(bodybytes);
//					buffer.flip();
//					logger.debug(buffer.dump());
////					data = decode.readObject();
//				} catch (Exception ex) {
//					data = ex;
//				}
//				body.setValue(data);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.debug("####end####");
	}
}
